package com.greenold.finance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.greenold.system.domain.WxChatCustomer;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.greenold.common.annotation.Log;
import com.greenold.common.core.controller.BaseController;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.common.enums.BusinessType;
import com.greenold.finance.domain.Bill;
import com.greenold.finance.service.IBillService;
import com.greenold.common.utils.poi.ExcelUtil;
import com.greenold.common.core.page.TableDataInfo;

/**
 * 入账列表Controller
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/finance/billList")
public class BillController extends BaseController
{
    @Autowired
    private IBillService billService;

    /**
     * 查询入账列表列表
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bill bill)
    {
        startPage();
        List<Bill> list = billService.selectBillList(bill);
        return getDataTable(list);
    }

    /**
     * 导出入账列表列表
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:export')")
    @Log(title = "入账列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bill bill)
    {
        List<Bill> list = billService.selectBillList(bill);
        ExcelUtil<Bill> util = new ExcelUtil<Bill>(Bill.class);
        util.exportExcel(response, list, "入账列表数据");
    }

    /**
     * 获取入账列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(billService.selectBillById(id));
    }

    /**
     * 新增入账列表
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:add')")
    @Log(title = "入账列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bill bill)
    {
        return toAjax(billService.insertBill(bill));
    }

    /**
     * 修改入账列表
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:edit')")
    @Log(title = "入账列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bill bill)
    {
        return toAjax(billService.updateBill(bill));
    }

    /**
     * 删除入账列表
     */
    @PreAuthorize("@ss.hasPermi('finance:billList:remove')")
    @Log(title = "入账列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(billService.deleteBillByIds(ids));
    }
    //根据账单号查询单个账单详情 使用拼接
    @GetMapping("/getBillDetailByBillNo")
    public AjaxResult getBillByBillNo(String billNo) {
        Bill bill =billService.selectBillByBillNo(billNo);
        if (bill != null)
        {
            return success(bill);
        }
        return error("该账单不存在");
    }
    @GetMapping("/getBillDetailByElderId")
    public AjaxResult getBillByElderId(String elderId) {
        System.out.println(elderId);
        List<Bill> bills = billService.selectBillByElderId(elderId);
        if (bills != null && !bills.isEmpty())
        {
            return success(bills);
        }
        return error("未查询到该老人的账单信息");
    }
    @GetMapping("/getBillListByUserId")
    public AjaxResult getBillListByUserId(@RequestParam String userId) {
       return success(billService.getBillListById(userId));
    }
}
